#include "qmyNalog.h"
#include "qFinNalvnes.h"
#include "qFinNalkorekcija.h"

QMyNalog::QMyNalog(QWidget *parent)
    : QMyBaseWidget(parent)
    ,NalogVnes(0)
    ,NalogKorekcija(0)
    ,NalogLista(0)
	,m_row(0)
{
    ui.setupUi(this);
    pressF4();
}

QMyNalog::~QMyNalog()
{

}
void QMyNalog::pressF2()
{
    closeAllForm();
    NalogVnes = showMyWidget<QNalogVnes>(NalogVnes); 
    NalogVnes->setCategoryWidget(this);
    connect(NalogVnes,SIGNAL(closeW()),this,SLOT(closeW()));
    connect(NalogVnes,SIGNAL(scallKomintenti(QWidget*, QString)),this,SLOT(callKomintenti(QWidget*, QString)));
    connect(NalogVnes,SIGNAL(scallArtikli(QWidget*, QString)),this,SLOT(callArtikli(QWidget*, QString)));
	emit disablePrint(true);
}
void QMyNalog::pressF3()
{
    if(!NalogLista)
    {
        return;
    }
    int id = NalogLista->getSelectedId();
    closeAllForm();
    NalogKorekcija = showMyWidget<QNalogKorekcija>(NalogKorekcija); 
    NalogKorekcija->setCategoryWidget(this);
    connect(NalogKorekcija,SIGNAL(closeW()),this,SLOT(closeW()));
    connect(NalogKorekcija,SIGNAL(scallKomintenti(QWidget*, QString)),this,SLOT(callKomintenti(QWidget*, QString)));
    connect(NalogKorekcija,SIGNAL(scallArtikli(QWidget*, QString)),this,SLOT(callArtikli(QWidget*, QString)));
    NalogKorekcija->init(id);
	emit disablePrint(true);
}
void QMyNalog::pressF4()
{
    closeAllForm();
    NalogLista = showMyWidget<QNalogLista>(NalogLista); 
    NalogLista->setCategoryWidget(this);
	NalogLista->setRow(m_row);
    connect(NalogLista, SIGNAL(closeW()), this, SLOT(closeWLista()));
    connect(NalogLista, SIGNAL(procF2()), this, SLOT(procF2()));
    connect(NalogLista, SIGNAL(procF3()), this, SLOT(procF3()));
    connect(NalogLista, SIGNAL(procF6()), this, SLOT(procF6()));
    connect(NalogLista, SIGNAL(procDel()), this, SLOT(procDel()));
	connect(NalogLista, SIGNAL(sFakturiToIzvod(QString&, QString&, QString&, QString&, izlFakturaData&)), this, SLOT(retFakturiToIzvod(QString&, QString&, QString&, QString&, izlFakturaData&)));
	emit disablePrint(false);
}

void QMyNalog::pressF6()
{
}

void QMyNalog::callProizvodi(QWidget* myobj, QString text)
{
    emit signProizvodiFromNalog(myobj, text);
}

void QMyNalog::procF2()
{
    pressF2();
}
void QMyNalog::procF3()
{
    pressF3();
}

void QMyNalog::procF6()
{
    pressF6();
}

void QMyNalog::refresh()
{
}

void QMyNalog::procDel()
{

}
void QMyNalog::pressEscape()
{
    this->raise();
    this->setFocus();
	emit disablePrint(true);
    emit signCloseMyWidget();
}
void QMyNalog::closeW()
{
    pressF4();
}
void QMyNalog::closeWLista()
{
    pressEscape();
}

void QMyNalog::closeAllForm()
{
    NalogKorekcija = deleteMyWidget<QNalogKorekcija>(NalogKorekcija);
    NalogVnes = deleteMyWidget<QNalogVnes>(NalogVnes);
	if (NalogLista)
	{
		m_row = NalogLista->getRow();
	}
    NalogLista = deleteMyWidget<QNalogLista>(NalogLista);
}
void QMyNalog::callKomintenti(QWidget* myobj, QString text)
{
    emit signKomintentiFromIspratnica(myobj, text);
}

void QMyNalog::callArtikli(QWidget* myobj, QString text)
{
    emit signArtikliFromIspratnica(myobj, text);
}

void QMyNalog::sendPrintMessage()
{
    emit signPrint();
}


QStandardItemModel* QMyNalog::getModel()
{
    QStandardItemModel * modelExt;
    if (NalogLista)
    {
        modelExt = NalogLista->model;
    } 
    else
    {
        modelExt = NULL;
    }
    return modelExt;
}

QStandardItemModel* QMyNalog::getModel2()
{
    QStandardItemModel * modelExt;
    if (NalogLista)
    {
        modelExt = NalogLista->model2;
    } 
    else
    {
        modelExt = NULL;
    }
    return modelExt;
}

QStringList QMyNalog::getIspratnicaInfo()
{
	QStringList modelExt;
	modelExt.clear();

	if (NalogLista)
	{
		modelExt = NalogLista->geInfo();
	} 
	else
	{
		modelExt;
	}
	return modelExt;
}

void QMyNalog::retFakturiToIzvod(QString& text6, QString& text8, QString& text9, QString& textName, izlFakturaData& mData)
{
	QWidget *sour = getSourceWidget();

	if (qobject_cast<QFinNalVnes*>(sour))
	{
		QFinNalVnes *dest = qobject_cast<QFinNalVnes*>(sour);
		dest->setNalogID((QMyBaseFormWidget::izlFakturaData&)mData);
		emit eupdateNanigator(dest->getCategoryWidget(), this);
		dest->setFocusLine2();
	}
	if (qobject_cast<QFinNalKorekcija*>(sour))
	{
		QFinNalKorekcija *dest = qobject_cast<QFinNalKorekcija*>(sour);
		dest->setNalogID((QMyBaseFormWidget::izlFakturaData&)mData);
		emit eupdateNanigator(dest->getCategoryWidget(), this);
		dest->setFocusLine2();
	}
}
